/****************************************************************************************
 * 
 *  Author:         CFY
 *  Date:           2025-07-09
 *  Description:    处理元宇宙的命令
 *  Version:        V1.1
 * 
****************************************************************************************/
#include "yyz_handler.h"

// 
/**
 * @brief           处理从元宇宙下发的指令
 * @param topic     主题
 * @param msg       要发送的消息
 * @return          成功    返回 0
 *                  失败    返回 -1           
 */
int cmd_handle(const char *payload, char *json_string)
{
    // 参数检查
    if (payload == NULL) {
        return -1;
    }
    char yyz_string[1024];

    int key;
    char name[32];
    char val[32];
    
    strcpy(yyz_string, payload);

    cJSON *yyz_root = cJSON_Parse(yyz_string);
    if(yyz_root == NULL)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if(error_ptr != NULL)
        {
            printf("[Error]: JSON Analysis failed! Near the [%ld] character!\n", error_ptr-yyz_string);
            printf("         %s\n", error_ptr);
        }
        else
        {
            printf("[Error] : cJSON_Parse() analysis failed!\n");
        }
        return -1;
    }

    cJSON *data;
    cJSON *json_root = cJSON_CreateObject();
    
    if((data = cJSON_GetObjectItem(yyz_root, "lamp")) != NULL)
    {
        cJSON_AddNumberToObject(json_root, "key", 1);
        cJSON_AddStringToObject(json_root, "name", "lamp");
        strcpy(val, (data->type==cJSON_True? "true" : "false"));
        cJSON_AddStringToObject(json_root, "data", val);
    }
    else if((data = cJSON_GetObjectItem(yyz_root, "fan")) != NULL)
    {
        cJSON_AddNumberToObject(json_root, "key", 2);
        cJSON_AddStringToObject(json_root, "name", "fan");
        strcpy(val, (data->type==cJSON_True? "true" : "false"));
        cJSON_AddStringToObject(json_root, "data", val);
    }
    else if((data = cJSON_GetObjectItem(yyz_root, "irrigated")) != NULL)
    {
        cJSON_AddNumberToObject(json_root, "key", 3);
        cJSON_AddStringToObject(json_root, "name", "irrigated");
        strcpy(val, (data->type==cJSON_True? "true" : "false"));
        cJSON_AddStringToObject(json_root, "data", val);
    }
    else
    {
        return -1;
    }

    char *string = cJSON_Print(json_root);
    strcpy(json_string, string);

    cJSON_Delete(json_root);
    cJSON_Delete(yyz_root);
}


// 将字符串转换为 元宇宙格式的JSON数据
int data_handle(const char *json_string, char *yyz_string)
{

    cJSON *root = cJSON_Parse(json_string);
    if(root == NULL)
    {
        printf("Json 解析失败\n");
        return -1;
    }
    cJSON *data = cJSON_GetObjectItem(root, "data");
    if(data == NULL)
    {
        printf("no data\n");
    }
    else
    {
        cJSON *yyz_root = cJSON_CreateObject();

        int count = cJSON_GetArraySize(data);
        for(int i = 0; i < count; i++)
        {
            cJSON *element = cJSON_GetArrayItem(data, i);

            cJSON *key = cJSON_GetObjectItem(element, "key");
            cJSON *name = cJSON_GetObjectItem(element, "name");
            cJSON *val = cJSON_GetObjectItem(element, "data");

            if(val->valuestring != NULL)
            {
                if(0 == strcmp("true", val->valuestring))
                {
                    cJSON_AddTrueToObject(yyz_root, name->valuestring);
                }
                else if(0 == strcmp("false", val->valuestring))
                {
                    cJSON_AddFalseToObject(yyz_root, name->valuestring);
                }
                
            }
            else if(val->type == cJSON_Number)
            {
                cJSON_AddNumberToObject(yyz_root, name->valuestring, val->valuedouble);
            }
        }
        char *string = cJSON_Print(yyz_root);
        strcpy(yyz_string, string);


        cJSON_Delete(yyz_root);
        cJSON_Delete(root);
    }
    return 0;
}





















